<template>
  <div>
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="$parent.open" width="780px" append-to-body>
      <div class="center_dialog">
        <el-form ref="form" :model="form" :rules="rules" label-width="110px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入资料名称"></el-input>
          </el-form-item>
          <el-form-item label="资料分类" prop="dataType">
            <el-select v-model="form.dataType" placeholder="请选择资料分类" style="width:100%" clearable>
              <el-option v-for="(dict, index) in dict.type.data_type" :key="index" :label="dict.label"
                :value="dict.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="适用对象" prop="fitObject">
            <el-input type="textarea" :rows="4" placeholder="请输入适用对象(选填)" v-model="form.fitObject">
            </el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" :rows="4" placeholder="请输入备注(选填)" v-model="form.remark">
            </el-input>
          </el-form-item>
          <el-form-item label="附件" prop="file">
            <FileUpload :isFile="true" v-model="form.file" :limit="1" @input="inputfile">
            </FileUpload>
          </el-form-item>
        </el-form>
      </div>
      <div align="center" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="() => $parent.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createdbase, updatedbase, getbasechildren } from "@/api/knowledge/index.js";
// import Upload from '@/components/upload/index.vue'
export default {
  props: ['title', 'rowObj'],
  dicts: ['data_type'],
  components: {
    // Upload
  },
  data () {
    return {

      form: {
        "name": undefined,      //名称
        "dataType": undefined,     //资料分类id-关联字典
        "fitObject": undefined, //适用对象
        "file": undefined,      //附件
        "fileName": undefined,  //附件名称
        "remark": undefined //备注
      },
      rules: {
        name: { required: true, message: '必填', trigger: ['blur', 'change'] },
        dataType: { required: true, message: '必填', trigger: ['blur', 'change'] },
      }
    }
  },
  computed: {
  },
  watch: {
  },
  created () {
    this.getlist()
  },
  methods: {
    inputfile (url) {
      if (url) {
        this.form.fileName = url[0].name
        this.form.file = url[0].url
      } else {
        this.form.fileName = ''
        this.form.file = ''
      }
    },
    /**初始化 */
    async getlist () {
      if (this.title === '修改资料') {
        let { data: data } = await getbasechildren(this.rowObj.id);
        this.form = data
      }
    },
    //提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '修改资料') {//修改
            let params = {
              ...this.form
            }
            updatedbase(params).then(res => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.$parent.open = false
                // this.$parent.$parent.$refs['search'].handleSelect()
                this.$parent.$parent.$refs['search'].resetQuery()
                // this.$parent.getList()//重新查询列表
              }
            })
          } else {//新增
            let params = {
              ...this.form
            }
            createdbase(params).then(res => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.$parent.open = false
                this.$parent.$parent.$refs['search'].resetQuery()
                // this.$parent.handleUpdate()
              }
            })
          }
        }
      });
    },
  }
}
</script>

<style lang="scss" scoped>
.center_dialog {
  // text-align: center;
  width: 60%;
  margin: 0px auto;
}
</style>